﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using CL.Business.Entity;
using CL.Dapper.Repository;
using CL.Enum.Common;
using Dapper;

namespace CL.Business.DAL
{
    public class NoticeDAL : DataRepositoryBase<NoticeEntity>
    {
        public NoticeDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {
        }
        /// <summary>
        /// 设置已读
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public bool ReadNotice(List<int> IDs)
        {
            using (IDbTransaction tran = base.db.BeginTransaction())
            {
                try
                {
                    IDs.ForEach((ID) =>
                    {
                        var Entity = base.Get(ID, tran);
                        Entity.IsRead = true;
                        base.Update(Entity);
                    });
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
        /// <summary>
        /// 设置已读
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public bool ReadNotice(int ID)
        {
            using (IDbTransaction tran =  base.db.BeginTransaction())
            {
                try
                {
                    var Entity = db.Get<NoticeEntity>(ID,tran);
                    Entity.IsRead = true;
                    db.Update(Entity,tran);
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
        /// <summary>
        /// 批量插入对象
        /// </summary>
        /// <param name="Entitys"></param>
        /// <returns></returns>
        public bool InsertNotice(List<NoticeEntity> Entitys)
        {
            using (IDbTransaction tran = base.db.BeginTransaction())
            {
                try
                {
                    Entitys.ForEach((Entity) =>
                    {
                        base.Insert(Entity, tran);
                    });
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertNotice(NoticeEntity Entity)
        {
            return base.Insert(Entity) ?? 0;
        }
        /// <summary>
        /// 查询未读数
        /// </summary>
        /// <param name="UnionID"></param>
        /// <returns></returns>
        public int QueryUnReadCount(int AccountID)
        {
            return base.RecordCount(new { AccountID = AccountID, IsRead = false });
        }

        /// <summary>
        /// 获取一条消息详情
        /// </summary>
        /// <param name="noticeID"></param>
        /// <returns></returns>
        public NoticeEntity SelectNoticeDal(long noticeID)
        {
            return base.Get(noticeID);
        }

        /// <summary>
        /// 查询公告列表
        /// </summary>
        /// <param name="AccountID">代理账号标识</param>
        /// <param name="IsRead">消息状态</param>
        /// <param name="Keyword">关键字</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="RecordCount">消息总数</param>
        /// <param name="UnReadCount">未读消息数量</param>
        /// <returns></returns>
        public List<NoticeEntity> QueryMyNotice(int AccountID, int IsRead, string Keyword, int PageIndex, int PageSize, ref int RecordCount, ref int UnReadCount)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@AccountID", AccountID, DbType.Int32);
            Parms.Add("@IsRead", IsRead, DbType.Int32);
            Parms.Add("@Keyword", Keyword, DbType.String);
            Parms.Add("@PageIndex", PageIndex, DbType.Int32);
            Parms.Add("@PageSize", PageSize, DbType.Int32);
            Parms.Add("@RecordCount", 0, DbType.Int32, ParameterDirection.Output, 4);
            Parms.Add("@UnReadCount", 0, DbType.Int32, ParameterDirection.Output, 4);
            var obj = base.db.Query<NoticeEntity>("udp_QueryNotice", Parms, null, true, null, CommandType.StoredProcedure);
            RecordCount = Parms.Get<int>("@RecordCount");
            UnReadCount = Parms.Get<int>("@UnReadCount");
            return obj.ToList();
        }



        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelEntity(int id)
        {
            return base.Delete(id) > 0;
        }

    }
}
